﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quản_Lý_Thư_Viện
{
    public partial class frmTheLoaiSach : Form
    {
        public frmTheLoaiSach()
        {
            InitializeComponent();
        }
        TheloaiControl control = new TheloaiControl();
        bool kt = false;
        bool bInserted = false;
        public static string timtl;

        private void frmTheLoaiSach_Load(object sender, EventArgs e)
        {
            dgvTheLoai.DataSource = control.LayDSTheloai();

        }
        private void Laythongtintheloai(ref TheloaiInfo info)
        {
            info.Matheloai = txtMaLoaiSach.Text;
            info.Tentheloai = txtTenTheLoai.Text;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            bInserted = true;
            Clear();
            ShowControl(true);
            btnSua.Enabled = false;
            btnThem.Enabled = false;
            int i = dgvTheLoai.Rows.Count + 1;
            txtMaLoaiSach.Text = "TL"+i+"";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            bInserted = false;
            ShowControl(true);
            btnSua.Enabled = false;
            btnThem.Enabled = false;
            
        }

        private void dgvTheLoai_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvTheLoai.RowCount > 0)
            {
                txtMaLoaiSach.Text = dgvTheLoai.CurrentRow.Cells["MATHELOAI"].Value.ToString();
                txtTenTheLoai.Text = dgvTheLoai.CurrentRow.Cells["TENTHELOAI"].Value.ToString();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            TheloaiInfo tlinfo = new TheloaiInfo();
            Laythongtintheloai(ref tlinfo);
            if ( txtTenTheLoai.Text == "")
            {
                MessageBox.Show("Chưa Nhập Đầy Đủ Các Thành Phần Yêu Cầu !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (bInserted)
                {



                    if (kiemtra(txtMaLoaiSach.Text.Trim()) == true)
                    {
                        MessageBox.Show("Trùng Mã Loại Sách !");
                    }
                    else
                    {
                        control.inserttheloai(tlinfo);
                        MessageBox.Show("Thêm Thể Loại Thành Công");
                        dgvTheLoai.DataSource = control.LayDSTheloai();
                        bInserted = false;
                        ShowControl(false);
                        btnSua.Enabled = true;
                        btnThem.Enabled = true;
                    }
                }

                else
                {
                    Laythongtintheloai(ref tlinfo);
                    control.updatetheloai(tlinfo);
                    MessageBox.Show("Sửa Dữ Liệu Thành Công");
                    dgvTheLoai.DataSource = control.LayDSTheloai();
                    ShowControl(false);
                    btnSua.Enabled = true;
                    btnThem.Enabled = true;
                }
            }
        }
        private Boolean kiemtra(string ma)
        {

            for (int j = 0; j < dgvTheLoai.RowCount ; j++)
            {
                if (ma == dgvTheLoai.Rows[j].Cells[0].Value.ToString())
                {
                    kt = true;
                    break;
                }
                else
                    kt = false;
            }
            return kt;
        }
        private void ShowControl(bool bShow)
        {
            //txtMaLoaiSach.Enabled = bInserted;
            txtTenTheLoai.Enabled = bShow;
            btnSave.Enabled = bShow;
        }
        private void Clear()
        {
            txtMaLoaiSach.Text = "";
            txtTenTheLoai.Text = "";
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (dgvTheLoai.RowCount > 0)
            {
                txtMaLoaiSach.Text = dgvTheLoai.CurrentRow.Cells["MATHELOAI"].Value.ToString();
                txtTenTheLoai.Text = dgvTheLoai.CurrentRow.Cells["TENTHELOAI"].Value.ToString();
            }
            bInserted = false;
            ShowControl(false);
            btnSua.Enabled = true;
            btnThem.Enabled = true;
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            timtl = textBox3.Text;
            dgvTheLoai.DataSource = control.timtl();
        }      
    }
}
